using UnityEngine;
using System.Collections;

public class bossAI : MonoBehaviour {
	public Transform bullet, sweeper, shockwave;
	public float fireDelay = 0.4f, fireDelay2 = 3.0f;
	public float lastFired = 0.0f, lastFired2 = 0.0f;
	public float curXT = 0.0f, curZT = 1.0f, curYT = 1.0f, zR = 0.0f, xR = 1.0f;
	public int arcDirection = 1, spaceDirection = 1;
	public GUITexture t1, t2;
	public playerManager pM;
	public EnemyHealth health;
	public Transform a,b,c,d, cur;
	// Use this for initialization
	void Start () {
		health = transform.GetComponent<EnemyHealth>();
		if(Network.isServer){
			pM = GameObject.Find("playerManager").GetComponent<playerManager>();
		}
	}
	// Update is called once per frame
	bool testCur(Transform test){
		Vector3 curTar = new Vector3(curXT, curYT, curZT);
		if(Vector3.Distance(transform.position,curTar) > Vector3.Distance(test.position,transform.position)){
			return true;
		}
		return false;
	}
	void Update () {
		if(Network.isServer){
			if(cur != null){
				curXT = cur.position.x;
				curYT = cur.position.y;
				curZT = cur.position.z;
			}
			if(pM.p1 == true){
				if(a == null){
					a = GameObject.Find("updatedPlayer(Clone)").transform;
				}
					if(testCur(a) == true){
						cur = a;
					}
			}
			else{
				a = null;
			}
			if(pM.p2 == true){
				if(b == null){
					b = GameObject.Find("updatedPlayer(Clone)").transform;
				}
					if(testCur(b) == true){
						cur = b;
					}
			}
			else{
				b = null;
			}
			if(pM.p3 == true){
				if(c == null){
					c = GameObject.Find("updatedPlayer(Clone)").transform;
				}
					if(testCur(c) == true){
						cur = c;
					}
			}
			else{
				c = null;
			}
			if(pM.p4 == true){
				if(d == null){
					d = GameObject.Find("updatedPlayer(Clone)").transform;
				}
					if(testCur(d) == true){
						cur = d;
					}
			}
			else{
				d = null;
			}
		}
		if(lastFired2 > 0.0f){
			lastFired2 -= Time.deltaTime;
		}
		if(lastFired2 <= 0.0f){
			Transform sW1 = (Transform)Network.Instantiate(shockwave, new Vector3(transform.position.x, 10, transform.position.z), transform.rotation,0);
			lastFired2 = fireDelay2;
		}
		if(lastFired > 0.0f)
			lastFired -= Time.deltaTime;
		if(lastFired <= 0.0f){
			Transform b1 = (Transform)Network.Instantiate(bullet, new Vector3(transform.position.x-50, 10, transform.position.z+50*zR),transform.rotation,0);
			Transform b2 = (Transform)Network.Instantiate(bullet, new Vector3(transform.position.x-50, 10, transform.position.z-50*zR),transform.rotation,0);
			Transform s1 = (Transform)Network.Instantiate(sweeper, new Vector3(transform.position.x-50, 10, transform.position.z-50),transform.rotation,0);
			Transform s2 = (Transform)Network.Instantiate(sweeper, new Vector3(transform.position.x-50, 10, transform.position.z+50),transform.rotation,0);
			b1.GetComponent<bulletScript>().setDest(new Vector3(-1,0,zR));
			b2.GetComponent<bulletScript>().setDest(new Vector3(-1,0,-zR));
			s1.GetComponent<sweeperScript>().setMinMax(-100.0f, 100.0f);
			s2.GetComponent<sweeperScript>().setMinMax(-100.0f, 100.0f);
			s1.GetComponent<sweeperScript>().setUpDown(-1);
			s2.GetComponent<sweeperScript>().setUpDown(1);
			if(xR >= 1.0f){
				spaceDirection = -1;
			}
			if(xR <= -1.0f){
				spaceDirection = 1;
			}
			xR += 0.1f*spaceDirection;
			if(zR >= 0.5f){
				arcDirection = -1;
			}
			else if(zR <= -0.5f){
				arcDirection = 1;
			}
			zR += 0.1f*arcDirection;
			lastFired = fireDelay;
		}
		if(Network.isServer)
			networkView.RPC("UpdateTestBossPosition", RPCMode.Others, transform.position, transform.rotation);
	}
	void OnGUI(){
		GUI.TextArea(new Rect(20,40,60,20), health.health.ToString());
	}
	[RPC]
	void UpdateTestBossPosition(Vector3 position, Quaternion rotation){
		transform.position = position;
		transform.rotation = rotation;
	}
	[RPC]
	void UpdateTestBossXT(float x){
		curXT = x;
	}
}
